<template>
  <div class="container" >
    <!-- 路由匹配到的组件将渲染在这里 -->
    <router-view></router-view>
  </div>
</template>

<script>
import Util from "util";
import API  from "api";
import wx  from 'weixin-js-sdk';
export default {

  created : function(){

     let userInfo,openId,userId;

     window.isInWx = Util.WX.isWX();

     //url获取openId  oo_NKw8UhOaypMc8bGQ_sybqFESs
     openId = Util.Location.getHashParamByName('openId');
     //url获取userId  14
     userId = Util.Location.getHashParamByName('userId');

     if(!openId){
          console.log("去授权")
          openId = Util.Storage.getLg("openId");
           //去授权
          //if(!openId){this.toWxOauth(); return;}
     }
     else{
         Util.Storage.setLg("openId",openId);
     }

     if(!userId){
        userId = Util.Storage.getLg("userId");
        if(!userId){Util.UI.toast("参数不匹配!");return;};
     }
     else{
        Util.Storage.setLg("userId",userId);
     }

     userInfo = Util.Storage.getLgObj("userInfo");

     //获得用户信息
     if(!userInfo){
        this.getUserInfo();
     }
     else{
        window.USER_INFO = userInfo;
     }

     this.getSignature();
  },

  methods : {

    //去授权
    toWxOauth : function(){
        window.forwardAuthPage();
    },

    //获得用户信息
    getUserInfo : function(){
        API.biz.queryUserBaseInfo()
          .then((res)=>{
              if(res.bizData)
                 console.log("用户信息获得成功!");
                 Util.Storage.setLgObj("userInfo",res.bizData);
                 window.USER_INFO = res.bizData;
          });
    },

     //获得jssdk验证
     getSignature: function(){
         //let url = window.location.href.split("#")[0]+this.$route.path.substring(1,this.$route.path.length);
         let url = window.location.href.split("#")[0];
         var params = {
             currentUrl : url,
         };

        API.wx.getJSTicket(params)
          .then((res)=>{
           wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: res.bizData.appId, // 必填，公众号的唯一标识
                timestamp: res.bizData.timestamp, // 必填，生成签名的时间戳
                nonceStr: res.bizData.noncestr, // 必填，生成签名的随机串
                signature: res.bizData.signature,// 必填，签名，见附录1
                jsApiList:
                  [ 'onMenuShareTimeline', 'onMenuShareAppMessage',
                    'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone',
                    'startRecord', 'stopRecord', 'onVoiceRecordEnd',
                    'playVoice', 'pauseVoice', 'stopVoice', 'onVoicePlayEnd',
                    'uploadVoice', 'downloadVoice', 'chooseImage',
                    'previewImage', 'uploadImage', 'downloadImage',
                    'translateVoice', 'getNetworkType', 'openLocation',
                    'getLocation', 'hideOptionMenu', 'showOptionMenu',
                    'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem',
                    'showAllNonBaseMenuItem', 'closeWindow', 'scanQRCode',
                    'chooseWXPay', 'openProductSpecificView', 'addCard',
                    'chooseCard', 'openCard' ]
            });
            wx.ready(this.onWxReady);
            wx.error(this.onWxError);
        });
    },

    onWxReady : function(){
      console.log("微信jssdk 加载成功!");

      //隐藏所有功能
      Util.WX.toggleMenu(false);
    },

    onWxError : function(err){
       console.error("微信jssdk 加载失败!",err);
    }

  },
  components: {

  }
}
</script>

<style lang="sass">
  @import "./lib/reset/h5_light.css";
  @import "./common/skin/template.scss";
</style>
